<?php
require_once ("secure_area.php");
class Config extends Secure_area 
{
	function __construct()
	{
		parent::__construct('config');
	}
	
	function index()
	{
		$this->load->view("config");
	}
	
	/**
	 *    Method is used to validate strings to allow alpha
	 *    numeric spaces underscores and dashes ONLY.
	 *    @param $str    String    The item to be validated.
	 *    @return BOOLEAN   True if passed validation false if otherwise.
	 */
	function _alpha_dash_space($str_in = '')
	{
		if (! preg_match("/^([-a-z0-9_ ])+$/i", $str_in))
		{
			$this->form_validation->set_message('_alpha_dash_space', 'The %s field may only contain alpha-numeric characters, spaces, underscores, and dashes.');
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}
		
	function save()
	{
		$this->form_validation->set_rules('store_id','Store ID','numeric|required');
		$this->form_validation->set_rules('company','Company','callback__alpha_dash_space|required');
		$this->form_validation->set_rules('rate','Rate','numeric|required');
		$this->form_validation->set_rules('convert_rate','Convert Rate','numeric|required');
		
		$batch_save_data=array(
		'store_id'=>$this->input->post('store_id'),
		'company'=>$this->input->post('company'),
		'print_after_sale'=>$this->input->post('print_after_sale'),
		'rate'=>$this->input->post('rate'),
		'convert_rate'=>$this->input->post('convert_rate')
		);
        
		if( !$this->form_validation->run() == FALSE && $this->Appconfig->batch_save( $batch_save_data ))
		{
			echo json_encode(array('success'=>true,'message'=>$this->lang->line('config_saved_successfully')));
		} else {
			echo json_encode(array('false'=>true,'message'=>'Please check input again!'));
		}
	}
}
?>